<template>
	<view class="">
		<u-navbar title-color="#fff" back-icon-color="#ffffff" :is-fixed="isFixed" :is-back="false"
			:background="background">
			<view class="slot-wrap" v-if="useSlot">
				<view class="search-wrap" v-if="search">
					<!-- 如果使用u-search组件，必须要给v-model绑定一个变量 -->
					<u-search v-model="keyword" action-text="搜索" :animation="true" :show-action="showAction" height="71"
						:action-style="{color: '#fff'}" @search="doSearch" @custom="doSearch">
					</u-search>
				</view>
			</view>
		</u-navbar>

		<view class="zg-weal-swiper">
			<u-swiper :list="hotWealPosts" :title="swipsTitle"  @click="swipToRead" ></u-swiper>
		</view>

		<view class="weekNews-container colCen borderBox">
			<view class="scroll-content borderBox">
				<scroll-view class="newScroll" scroll-x="true" scroll-with-animation>
					<view class="listcontent">
						<block v-for="(items,index) in nowWealPosts" :key="index">
							<view class="news-items borderBox" @click="toRead(items.articleId,items.articleType)">
								<view class="items-container colCen">
									<view class="newsPic defIcon">
										<image :src="items.articleImg" mode="aspectFill" :lazy-load="true"></image>
									</view>
									<view class="bottom-info-container colCen borderBox">
										<view class="newstitle">
											{{items.articleTitle}}
										</view>
									</view>
								</view>
							</view>
						</block>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="zg-weal-hot">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="catTabTitle" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item" v-for="(item1, index1) in catTabContent" :key="index1">
					<scroll-view scroll-y style="height: 100%;width: 100%;">
						<view class="item-container">
							<view v-for="(item2, index2) in item1" :key="index2" @click="setCatId(item2.catId)" :class="item2.catId==catId?'thumb-box ch':'thumb-box'">
								<image class="item-menu-image" :src="item2.catIcon" mode=""></image>
								<view class="item-menu-name">{{item2.catName}}</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>

			</swiper>

		</view>

		<view class="zg-weal-list">
			<view class="order" v-for="(item, index) in  wealPosts" :key="index">
				<view class="item" @click="toRead(item.articleId,item.articleType)">
					<view class="left">
						<image :src="item.articleImg" mode="aspectFill"></image>
					</view>
					<view class="content">
						<view class="title u-line-2">{{ item.articleTitle }}</view>
						<view class="type">111</view>
						<view class="delivery-time">发布时间：</view>
					</view>
				</view>
			</view>
		</view>

		<view>
			<u-loadmore :status="status" :loadText="loadText" />
		</view>
		<view class="wrap">
			<u-back-top :scroll-top="scrollTop"></u-back-top>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swipsTitle: true,
				catTabTitle: [],
				catTabContent: [],
				wealPosts: [],
				formData: {
					status: 'loading', // 加载状态
				},
				tipShow: false // 是否显示顶部提示框
					,
				// 福利分类
				wealCats: [],
				msg: [
					'寒雨连江夜入吴',
					'平明送客楚山孤',
					'洛阳亲友如相问',
					'一片冰心在玉壶'
				],
				//最新发布福利文章
				nowWealPosts: [], //推荐福利文章
				hotWealPosts: [],
				scrollTop: 0,
				showTip: true,
				backIconName: 'nav-back',
				showAction: true,
				useSlot: true,
				background: {
					'background-color': '#54547d'
				},
				isBack: true,
				search: true,
				custom: false,
				isFixed: true,
				keyword: '',
				pages: 1, //总页数
				pageNum: 1, //当前页,
				pageSize: 5, //请求条数
				catId: null,
				status: 'loadmore',
				loadText: {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0

			}
		},
		onLoad(options) {
			this.keyword = options.keyword;
			this.getWealCats();
			this.getHotWealPosts();
			this.getNowWealPosts();
			this.getWealPosts();

		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			if (this.pageNum > this.pages) {
				this.status = 'nomore';
				return;
			}
			this.pageNum = this.pageNum + 1;
			setTimeout(() => {
				if (this.pageNum > this.pages) {
					this.status = 'nomore';
				} else {
					this.status = 'loading';
					this.getWealPosts();
				}
			}, 50)
		},
		computed: {

		},
		methods: {
			//福利分类
			getWealCats() {
				this.$http.get('api/category/tree/24').then((res) => {
					let ts = [],
						tb = [];
					res.childList.forEach(function(value, index) {
						ts.push({
							'name': value.catName,
							'catId': value.catId
						});
						tb.push(value.childList);
					})
					this.catTabTitle = ts;
					this.catTabContent = tb;
				})

			},
			getPostImg(articleImg) {	
				if (articleImg==null||articleImg.length==0) {	
					return "/static/images/info-bg.png";
				} else {
					return articleImg.split(",")[0];
				}
			},
			//热门推荐福利
			getHotWealPosts() {
				let zt=this;
				this.$http.get('api/article/list/tag/1/1/10',{
					articleType: 1,
					pageSize: 10,
					pageNum: 1,
					orderByColumn:"article_top",
					isAsc:"desc"
				}).then((res) => {
					let ts = [];
					res.forEach(function(value, index) {
						ts.push({
							'title': value.articleTitle,
							'image': zt.getPostImg(value.articleImg),
							'articleId': value.articleId,
							'articleType': value.articleType
						});
					})

					this.hotWealPosts = ts;
				})
			},
			//最近发布福利
			getNowWealPosts() {
				this.$http.get('api/article/list/tag/1/1/10').then((res) => {

					this.nowWealPosts = res;
				})
			},
			//福利列表综合
			getWealPosts() {
				this.$http.get('api/article/list', {
					articleType: 1,
					articleTitle: this.keyword,
					catId: this.catId,
					pageSize: this.pageSize,
					pageNum: this.pageNum,
				}).then((res) => {
					if (res.rows.length > 0) {
						this.pageNum = res.pageNum;
						this.pages = res.pages;
						this.wealPosts = this.wealPosts.concat(res.rows);
						if (this.pageNum >= this.pages) {
							this.status = 'nomore';
						}
					} else {
						this.pageNum = 0;
						this.pages = 0;
						this.status = 'nomore';
					}

				})
			},
			doSearch() {
				this.wealPosts = [];
				this.getWealPosts();
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			toRead(articleId, articleType) {
				let u = '';
				if (articleType == 1) {
					u = "/pages/weal/post/post?articleId=" + articleId
				} else {
					u = "/pages/course/post/post?articleId=" + articleId
				}
				uni.navigateTo({
					url: u
				});
			},
			swipToRead(index) {
				this.toRead(this.hotWealPosts[index].articleId, this.hotWealPosts[index].articleType);
			},
			setCatId(catId){
				if(this.catId==catId)
				{
					this.catId=null;
				}
				else{
				this.catId=catId;	
				}
				this.pageNum=1;
				this.wealPosts=[];
				this.getWealPosts();
			}

		}
	}
</script>

<style lang="scss" scoped>
	@import '@/common/uni-ui.scss';

     .ch{
	
		 border: 2rpx solid #ebe2de;
	 }
    
	.zg-hotCats {
		border-bottom: 1rpx solid #FFF;
		display: flex;
		flex-direction: column;
		width: 100%;
	}


	.order {
		width: auto;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;
	}

	.item {
		display: flex;
		margin: 20rpx 0 0;

		.left {
			margin-right: 20rpx;

			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}

		.content {
			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}

			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}

			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}

		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;

			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}

			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}

	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #efeff4;
		min-height: 100%;
		height: auto;
	}

	/deep/.swiper-box {
		height: 190rpx;
	}

	.uni-list-box {
		padding: 20rpx;
	}

	.uni-content {
		padding-right: 15rpx;

	}

	.uni-content .zg-title {
		font-size: 35rpx;
	}

	.uni-note {
		display: flex;
		margin: 0;
		justify-content: space-between;
	}

	.tips {
		color: #67c23a;
		font-size: 14px;
		line-height: 40px;
		text-align: center;
		background-color: #f0f9eb;
		height: 0;
		opacity: 0;
		transform: translateY(-100%);
		transition: all 0.3s;
	}

	.tips-ani {
		transform: translateY(0);
		height: 40px;
		opacity: 1;
	}



	.list-picture {
		width: 100%;
		height: 145px;
	}

	.thumb-image {
		width: 100%;
		height: 100%;
	}

	.ellipsis {
		display: flex;
		overflow: hidden;
	}

	.uni-ellipsis-1 {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.uni-ellipsis-2 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.zg-weal-hot {
		padding-top: 10rpx;
		padding-bottom: 10rpx;
	}

	.u-th {
		background-color: #fff;
		border: none;
	}

	.zg-weal-swiper {
		padding: 5rpx;
	}

	.navbar-right {
		margin-right: 24rpx;
		display: flex;
	}

	.search-wrap {
		margin: 0 20rpx;
		flex: 1;
	}

	.right-item {
		margin: 0 12rpx;
		position: relative;
		color: #ffffff;
		display: flex;
	}

	.message-box {}

	.slot-wrap {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.map-wrap {
		display: flex;
		align-items: center;
		padding: 4px 6px;
		background-color: rgba(240, 240, 240, 0.35);
		color: #fff;
		font-size: 22rpx;
		border-radius: 100rpx;
		margin-left: 30rpx;
	}

	.map-wrap-text {
		padding: 0 6rpx;
	}


	.item-container {
		background-color: #fff;
		border-top: 1rpx solid #f1f1f1;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
	}

	.thumb-box {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		padding: 5rpx;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}

	.scroll-content {
		width: 100%;
		padding: 10rpx;
		margin: 10rpx;
		background-color: #fff;

		.newScroll {
			width: 100%;
			height: 190rpx;

			.listcontent {
				white-space: nowrap;
				height: 100%;

				.news-items {
					display: inline-block;
					margin-right: 20rpx;

					.items-container {
						width: 335rpx;
						border-radius: 10rpx;
						margin-top: 10rpx;

						.newsPic {
							width: 100%;
							height: 140rpx;
						}

						.bottom-info-container {
							width: 100%;
							height: 120rpx;
							padding: 0 20rpx;
							background-color: #292c404d;

							.newstitle {
								width: 100%;
								font-size: 24rpx;
								font-weight: 400;
								color: #333333;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
								line-height: 20px;
							}

							.avatorType-content {
								width: 100%;
								margin-top: 10rpx;

								.avatar {
									width: 60rpx;
									height: 60rpx;
									border-radius: 50%;
									overflow: hidden;
								}

								.type-box {
									font-size: 24rpx;
									font-weight: 400;
									color: #999999;
									margin-left: 10rpx;
								}
							}
						}
					}
				}
			}
		}
	}

	.container-title {
		width: 100%;
		height: 80rpx;
		font-size: 28rpx;
		font-weight: 500;
		color: #333333;
		padding: 0 16rpx;
	}
</style>
